package cn.demo.cm.service.impl;

import java.util.List;

import cn.demo.common.utils.StringUtils;
import com.mybatisflex.core.paginate.Page;
import cn.demo.common.core.page.PageQuery;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import cn.demo.cm.mapper.CmContentMapper;
import cn.demo.cm.domain.CmContent;
import cn.demo.cm.service.ICmContentService;

/**
 * 文章管理Service业务层处理
 *
 * @author xtjun
 * @date 2024-06-06
 */
@Service
public class CmContentServiceImpl extends ServiceImpl<CmContentMapper, CmContent> implements ICmContentService {
    @Resource
    private CmContentMapper cmContentMapper;
    /**
     * 查询文章管理
     *
     * @param id 文章管理主键
     * @return 文章管理
     */
    @Override
    public CmContent selectCmContentById(String id) {
        return cmContentMapper.selectOneById(id);
    }

    /**
     * 查询文章管理列表
     *
     * @param cmContent 文章管理
     * @return 文章管理
     */
    @Override
    public Page<CmContent> selectCmContentPage(CmContent cmContent, PageQuery pageQuery) {
        return cmContentMapper.paginate(pageQuery.build(), getQueryWrapper(cmContent));
    }

    @Override
    public List<CmContent> selectCmContentList(CmContent cmContent) {
        return cmContentMapper.selectListByQuery(getQueryWrapper(cmContent));
    }

    private static QueryWrapper getQueryWrapper(CmContent cmContent) {
        String lat = cmContent.getLat();
        String lng = cmContent.getLng();
        cmContent.setLat(null);
        cmContent.setLng(null);
        QueryWrapper queryWrapper = QueryWrapper.create(cmContent, CmContent.operators);
        if (StringUtils.isNotEmpty(lat) && StringUtils.isNotEmpty(lng)){
            queryWrapper.select("(6371 * acos(cos(radians("+lat+")) * cos(radians(lat)) * cos(radians(lng) - radians("+lng+")) + sin(radians("+lat+")) * sin(radians(lat)))) as distance").orderBy("distance",true);
        }
        return queryWrapper;
    }

    /**
     * 新增文章管理
     *
     * @param cmContent 文章管理
     * @return 结果
     */
    @Override
    public int insertCmContent(CmContent cmContent) {
        cmContent.setContentHit(0L);
        return cmContentMapper.insert(cmContent);
    }

    /**
     * 修改文章管理
     *
     * @param cmContent 文章管理
     * @return 结果
     */
    @Override
    public int updateCmContent(CmContent cmContent) {
        return cmContentMapper.update(cmContent);
    }

    /**
     * 批量删除文章管理
     *
     * @param ids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteCmContentByIds(List<String> ids) {
        return cmContentMapper.deleteBatchByIds(ids);
    }

    /**
     * 删除文章管理信息
     *
     * @param id 文章管理主键
     * @return 结果
     */
    @Override
    public int deleteCmContentById(String id) {
        return cmContentMapper.deleteById(id);
    }

    @Override
    @Async
    public void addViewHit(String id) {
        UpdateChain.of(CmContent.class)
                .setRaw(CmContent::getContentHit, "content_hit + 1")
                .where(CmContent::getId).eq(id)
                .update();
    }
}
